package com.hqyj.service.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.hqyj.dao.AnswerSheetMapper;
import com.hqyj.dao.QuestionMapper;
import com.hqyj.dao.ScoreManagementMapper;
import com.hqyj.pojo.AnswerSheet;
import com.hqyj.pojo.Question;
import com.hqyj.pojo.ScoreManagement;
import com.hqyj.service.AnswerSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 答题记录表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-11-19
 */
@Service
public class AnswerSheetServiceImpl implements AnswerSheetService {

    /**
     * 注入 AnswerSheetMapper 接口
     */
    @Autowired
    private AnswerSheetMapper answerSheetMapper;

    /**
     * 注入QuestionMapper
     */
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 注入 ScoreManagementMapper
     */
    @Autowired
    private ScoreManagementMapper scoreManagementMapper;

    @Override
    public HashMap<String, Object> submitPaper(AnswerSheet answerSheet) {
        HashMap<String, Object> map = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        System.err.println(answerSheet);
        List<AnswerSheet> answerList = answerSheet.getAnswerList();
        //System.out.println(answerList);
        AnswerSheet sheetTemp = new AnswerSheet();
        // 设置考生Id
        sheetTemp.setStuId(answerSheet.getStuId());
        // 判断考生有没有答题直接提交试卷
        if (answerList.size() > 0) {
            try {
                for (AnswerSheet sheet : answerList) {
                    //System.err.println("登记对象"+ sheet);
                    // 设置试卷Id
                    sheetTemp.setExamId(sheet.getExamId());
                    // 设置考生Id
                    //sheetTemp.setStuId(sheet.getStuId());
                    // 设置题目Id
                    sheetTemp.setQuestionId(sheet.getQuestionId());
                    // 设置考生 的 答案
                    sheetTemp.setStuAnswer(sheet.getStuAnswer());
                    // 查询当前题目的正确答案和分数
                    Question question = questionMapper.selectById(sheetTemp.getQuestionId());
                    // 设置正确答案
                    sheetTemp.setRightAnswer(question.getQuestionAnswer());
                    // 设置题目分数
                    sheetTemp.setQuestionScore(question.getQuestionScore());
                    // 考生答案
                    String stuAnswer = sheetTemp.getStuAnswer();
                    // 正确答案
                    String rightAnswer = sheetTemp.getRightAnswer();

                    //System.err.println("比较考生答案"+ sheetTemp.getStuAnswer() +"和正确答案"
                    //        + question.getQuestionAnswer() + "==" +
                    //        (stuAnswer.equals(rightAnswer)));

                    if (stuAnswer.equals(rightAnswer)) {
                        // 考生答案正确
                        sheetTemp.setAnswerScore(sheetTemp.getQuestionScore());
                    } else {
                        // 答案不正确
                        sheetTemp.setAnswerScore(0);
                    }

                    // 添加答题记录
                    int insert = answerSheetMapper.insert(sheetTemp);
                    //int insert = 1;
                    if (insert > 0) {
                        ;
                    } else {
                        throw new Exception();
                    }
                }
                //System.out.println("循环结束");
                AnswerSheet stuScore = answerSheetMapper.selectStuScore(sheetTemp.getStuId(), sheetTemp.getExamId());
                System.out.println("学生成绩==" + stuScore.toString());
                // 存储考生成绩的对象
                ScoreManagement scoreManagement = new ScoreManagement();
                // 设置试卷Id
                scoreManagement.setExamId(answerSheet.getExamId());
                // 设置考生Id
                scoreManagement.setStudentId(answerSheet.getStuId());
                // 设置考生成绩
                scoreManagement.setScore(stuScore.getAnswerScore());

                System.err.println("成绩信息=="+scoreManagement);
                // 登记考生成绩
                int result = scoreManagementMapper.insert(scoreManagement);
                //int result = 1;
                if (result > 0) {
                    // 返回考生的成绩提示信息
                    map.put("score", "你的成绩是 " + scoreManagement.getScore() + " 分");
                } else {
                    map.put("score", "");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // 未选择任何答案提交,保存考试记录
            // 设置试卷编号
            sheetTemp.setExamId(answerSheet.getExamId());
            // 设置考生编号
            sheetTemp.setStuId(answerSheet.getStuId());
            // 设置 答题得分 0 分
            sheetTemp.setAnswerScore(0);
            // 考试记录表 插入当前考试记录
            int result = answerSheetMapper.insert(sheetTemp);

            // 登记成绩为0
            ScoreManagement score = new ScoreManagement();
            // 设置试卷编号
            score.setExamId(sheetTemp.getExamId());
            // 设置考生编号
            score.setStudentId(sheetTemp.getStuId());
            // 设置成绩
            score.setScore(0);
            // 登记考生成绩
            int num = scoreManagementMapper.insert(score);

            if (result > 0 && num > 0) {
                map.put("score", "你的成绩是 " + 0 + " 分");
            } else {
                map.put("score", "");
            }
        }
        return map;
    }
}
